package com.sduept.nwld.dataserver.controller.message;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.message.manager.MessageReceiveConfigManager;
import com.sduept.nwld.dataserver.manager.message.MessageReceiverManager;
import com.sduept.nwld.dataserver.model.library.MessageReceiver;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.List;


/**
 * 抽象字典controller
 * @author     Author
 * @version    V1.0
 * @Date       2018年8月23日 上午9:24:25
 */
@Named
@ViewScoped
public class MsgReceiverController extends AbstractController{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7968598509900581658L;

	@Autowired
	private MessageReceiverManager manager;
	@Autowired
	private MessageReceiveConfigManager relationManager;
	
	private MessageReceiver current;
	private MessageReceiver selected;
	private List<MessageReceiver> records=new ArrayList<MessageReceiver>();
	private boolean isCreate;
	private boolean isSave;
	
	@PostConstruct
	public void init(){
		records=manager.listAll();
	}

	public void preCreate() {
		current = new MessageReceiver();
		isCreate = true;
		isSave = false;
	}
	
	public void preUpdate(){
		current=selected;
		isCreate=false;
		isSave=true;
	}
	
	public void save(){
		if(StringUtils.isEmpty(current.getId())){
			addErrorMessage("唯一编码不能为空！");
		}
		if(isCreate){
			records.add(current);
		}
		manager.save(current);
		addSuccessMessage("保存成功！");
	}
	
	public boolean chekCodeExist() {
		if (StringUtils.isEmpty(current.getId())) {
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, "错误","唯一编码不能为空！"));
			isSave=false;
			return false;
		}
		MessageReceiver library = manager.getById(current.getId());
		if (null!=library) {
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, "错误","该唯一编码已被占用！"));
			isSave=false;
			return false;
		} 
		FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO, "提示","该唯一编码可用！"));
		isSave=true;
		return true;	
	}
	public void delete(){
		relationManager.delete(null, null, selected.getId());
		manager.deleteById(selected.getId());
		records.remove(selected);
		selected=null;
		addSuccessMessage("删除成功！");
	}

	public MessageReceiver getCurrent() {
		return current;
	}

	public void setCurrent(MessageReceiver current) {
		this.current = current;
	}

	public MessageReceiver getSelected() {
		return selected;
	}

	public void setSelected(MessageReceiver selected) {
		this.selected = selected;
	}

	public List<MessageReceiver> getRecords() {
		return records;
	}

	public void setRecords(List<MessageReceiver> records) {
		this.records = records;
	}

	public boolean isCreate() {
		return isCreate;
	}

	public void setCreate(boolean isCreate) {
		this.isCreate = isCreate;
	}

	public boolean isSave() {
		return isSave;
	}

	public void setSave(boolean isSave) {
		this.isSave = isSave;
	}
	

	
}
